from constants import *

from appengine_utilities import sessions
import logging

class MyUser:
    def __init__(self):
        self.sess = sessions.Session()
        if self.sess.has_key("username"):
            self.__loggedinData()
        else:
            self.__loggedoutData()

    def logout(self):
        self.sess.delete()
        self.__loggedoutData()

    def login(self, user):
        self.sess['username'] = user.username
        self.sess['admin'] = user.role == USER_ADMIN
        self.sess['tenant'] = user.role == USER_TENANT
        self.sess['landlord'] = user.role == USER_LANDLORD
        self.sess['role'] = user.role
        self.sess['key'] = str(user.key())
        self.__loggedinData()

    def __loggedinData(self):
        self.url = "/logout"
        self.loggedIn = 1
        self.url_text = 'Logout'

        self.username = self.sess['username']
        self.admin = self.sess['admin']
        self.tenant = self.sess['tenant']
        self.role = self.sess['role']
        self.landlord = self.sess['landlord']
        self.key = self.sess['key']

    def __loggedoutData(self):
        self.url = "/login"
        self.url_text = 'Login'
        self.admin = 0
        self.loggedIn = 0